/**
 * Created by zy on 2016/6/13.
 * 新建 - 阴保整改 - 腐蚀控制
 */

;(function ($, window, document, undefined) {

    // 管道
    var $pipeline = $('#JQ_pipeline').select3();
    // 管段
    var $segment = $('#JQ_segment').select3({
        title: '管段'
    });
    // 起点位置
    var $begin = $('#JQ_begin').select3({
        title: '起点位置',
        server: true,
        param: {'pipeLineId': $pipeline, 'pipeSegmentId': $segment}
    });
    // 终点位置
    var $end = $('#JQ_end').select3({
        title: '终点位置',
        server: true,
        param: {'pipeLineId': $pipeline, 'pipeSegmentId': $segment, 'beginSignId': $begin}
    });
    // 开始时间
    var $planStartDate = $('#JQ_planStartDate');
    // 结束时间
    var $planCompletionDate = $('#JQ_planCompletionDate');
    // 整改项目名称
    var $name = $('#JQ_name');
    // 施工单位
    var $company = $('#JQ_company');
    // 触发管道
    $pipeline.element.on('select', function (e, v) {
        var id = v || $pipeline.val();
        if (id) {
            DyyBase.ajax(DyyBase.url_pipe_pos(), {pipeLineId: id}, function (dataset) {
                var begin = DyyBase.getPos(dataset, 'begin');
                var end = DyyBase.getPos(dataset, 'end');
                $begin.asset(begin);
                $end.asset(end);
            });
        } else {
            $begin.add([]);
            $end.add([]);
        }
    });
    // 隐藏提交表单
    var $form = $('#JQ_cath_form_file');
    // 隐藏反馈iframe
    var ifame = $('#JQ_cath_iframe').get(0);
    // 导入文件input
    var $export_name = $('#JQ_export_name');

    // 表单对象
    var filter = {
        getData: function(tag){
            var data = {};
            data.mode = true;
            data.id = function(){
                var v = $('#JQ_repairTaskId').val();
                if(v){
                    return v;
                }
                return '';
            }();
            data.pipeLineId = function(){
                var v = $pipeline.val();
                if(v){
                    return v;
                }else{
                    $pipeline.element.TIP({content: '管道名称不能为空!'});
                    data.mode = false;
                }
                return '';
            }();
            data.fromPointId = function(){
                var v = $begin.val();
                if(v){
                    return v;
                }
                return '';
            }();
            data.toPointId = function(){
                var v = $end.val();
                if(v){
                    return v;
                }
                return '';
            }();
            data.planStartDate = function(){
                var v = $planStartDate.val();
                if(v){
                    return v;
                }else{
                    $planStartDate.TIP({content: '开始时间不能为空!'});
                    data.mode = false;
                }
                return '';
            }();
            data.planCompletionDate = function(){
                var v1 = $planStartDate.val();
                var v2 = $planCompletionDate.val();
                if(v2){
                    if(!(Dyy.checkDateLate(v1,v2))){
                        $planCompletionDate.TIP({content: '结束时间应当晚于开始时间!'});
                        data.mode = false;
                    }
                    return v2;
                }else{
                    $planCompletionDate.TIP({content: '结束时间不能为空!'});
                    data.mode = false;
                }
                return '';
            }();
            data.name = function(){
                var v = $name.val();
                if(v){
                    return v;
                }else{
                    $name.TIP({content: '整改项目名称不能为空!'});
                    data.mode = false;
                }
                return '';
            }();
            data.assemblyCompany = function(){
                var v = $company.val();
                if(v){
                    return v;
                }else{
                    $company.TIP({content: '施工单位不能为空!'});
                    data.mode = false;
                }
                return '';
            }();
            data.dataStated = function(){
                if(tag == 'save'){
                    return '01';
                }else if(tag == 'release'){
                    return '20';
                }
            }();
            data.export_name = function(){
                var v = $export_name.val();
                if(v){
                    return v;
                }else{
                    $export_name.TIP({content:'整改建议导入文件不能为空!'});
                    data.mode = false;
                }
            }();
            return data;
        }
    };


    /** 编辑时 - start **/
    var repairTaskId = $('#JQ_repairTaskId').val();
    if(repairTaskId){
        $('#JQ_action_step').text('编辑');
        $export_name.val('您还可以修改上传文件');
        DyyBase.ajax(corrctrlApi.url_cath_details(), {repairTaskId:repairTaskId}, function(dataset){
            if(dataset.bool){
                var task = dataset.result.repairTask;
                $pipeline.fit(task.pipeLineId);
                $begin.asset([{id:task.fromPointId,text:task.fromPointName,mile:task.fromPointMileage}]);
                $end.asset([{id:task.toPointId,text:task.toPointName,mile:task.toPointMileage}]);
                $name.val(task.name);
                $company.val(task.assemblyCompany);
                $planStartDate.val(Dyy.getDate(task.planStartDate,true));
                $planCompletionDate.val(Dyy.getDate(task.planCompletionDate,true));
            }
        });
    }
    /** 编辑时 - end **/


    // 保存
    $('#JQ_save_btn').click(function(){
        // form 提交
        var param = filter.getData('save');
        if(param.mode){
            $form.find('input[type="hidden"]').remove();
            for(var k in param){
                var $input = $('<input type="hidden">');
                $input.prop('name',k).val(param[k]);
                $form.append($input);
            }

            DyyBase.isLogin(function(){
                $form.submit();
            });
        }
        // form 提交返回结果处理
        ifame.onload = function(){
            var current = ifame.contentWindow;
            var body = current.document.body.innerHTML;
            try{
                if(body){
                    body = $.parseJSON(body);
                    if(body.bool){
                        $(this).MSG({
                            type: 'success',
                            content: $('#JQ_action_step').text() + '阴保整改项目成功!',
                            yesBtnAct: DyyBase.goToPage,
                            closeBtnAct: DyyBase.goToPage,
                            goToUrl: DyyBase.root_path + '/corrctrl/CathRepairTask/init.html'
                        });
                    }else{
                        $(this).MSG({
                            type: 'error',
                            content: body.result
                        });
                    }
                }
            }catch (e){
                $(this).MSG({
                    type: 'error',
                    content: e.message
                });
            }
        }
    });

    // 发布
    $('#JQ_release_btn').click(function(){
        // form 提交
        var param = filter.getData('release');
        if(param.mode){
            $form.find('input[type="hidden"]').remove();
            for(var k in param){
                var $input = $('<input type="hidden">');
                $input.prop('name',k).val(param[k]);
                $form.append($input);
            }
            $form.submit();
        }

        // form 提交返回结果处理
        ifame.onload = function(){
            var current = ifame.contentWindow;
            var body = current.document.body.innerHTML;
            try{
                if(body){
                    body = $.parseJSON(body);
                    if(body.bool){
                        $(this).MSG({
                            type: 'success',
                            content: '新建阴保整改项目成功!',
                            yesBtnAct: DyyBase.goToPage,
                            closeBtnAct: DyyBase.goToPage,
                            goToUrl: DyyBase.root_path + '/corrctrl/CathRepairTask/init.html'
                        });
                    }else{
                        $(this).MSG({
                            type: 'error',
                            content: body.result
                        });
                    }
                }
            }catch (e){
                $(this).MSG({
                    type: 'error',
                    content: e.message
                });
            }
        }
    });

    // 浏览文件
    $('#JQ_bower_file').change(function(){
        var $this = $(this);
        var value = $this.get(0).value;
        if(value){
            $('#JQ_export_name').val(value);
        }
    });

    // 模板下载
    $('#JQ_download').click(function(){
        var $this = $(this);
        $('#JQ_template_download').submit();
        var $iframe = $('#JQ_template_iframe');
        $iframe.get(0).onload = function(){
            try{
                var content = this.contentWindow.document.body.innerHTML;
                content = $.parseJSON(content);
                if(content.bool){

                }else{
                    $this.MSG({
                        type: 'error',
                        content: content.result
                    });
                }
            }catch(e){
                $this.MSG({
                    type: 'error',
                    content: e.message
                });
            }
        };
    });

    // 返回
    $('#JQ_goback_btn').click(function(){
        window.location.href = DyyBase.root_path + '/corrctrl/CathRepairTask/init.html';
    });

})(jQuery, window, document);